using System.Collections;
using BCW;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using I2.Loc;
using UnityEngine;

public class LeagueStats : MonoBehaviour
{
	[SerializeField]
	protected UIBasicSprite leagueTexture;

	[SerializeField]
	protected UILabel leagueName;

	[SerializeField]
	protected UILabel cupsCount;

	[SerializeField]
	protected UILabel newCupsCount;

	[SerializeField]
	protected GameObject x2bonus;

	protected int starstCups;

	protected League startLeague;

	protected int startLeagueIndex;

	protected bool _initialized;

	[HideInInspector]
	public int cups;

	[HideInInspector]
	public int addCups;

	public UILabel CupsCount
	{
		get
		{
			return cupsCount;
		}
	}

	private void Awake()
	{
		PanelResultRewardUI.giveReward += RefresStars;
	}

	private void OnDestroy()
	{
		PanelResultRewardUI.giveReward -= RefresStars;
	}

	private void RefresStars()
	{
		CoroutineRunner.Instance.StartCoroutine(CupsCountAnimation(CupsManager.Cups, 0));
	}

	private void OnEnable()
	{
		if (!_initialized)
		{
			Init();
		}
	}

	public void Init()
	{
		if (leagueTexture != null)
		{
			leagueTexture = leagueTexture.ReplaceForCurrentPlatform();
			leagueTexture.LoadTextureAndAlphaMask("LigasIcons/", CupsManager.CurrentLeague.ToString());
		}
		if (leagueName != null)
		{
			leagueName.text = ScriptLocalization.Get("Game_Common/" + CupsManager.CurrentLeague);
		}
		starstCups = CupsManager.Cups;
		startLeague = CupsManager.CurrentLeague;
		startLeagueIndex = CupsManager.CurrentLeagueIndex;
		_initialized = true;
	}

	public InfoMatchReward AnimateCups(int bonusFactorAward = 1)
	{
		int num = CupsManager.Cups - starstCups;
		CupsManager.Cups = starstCups;
		CupsManager.CurrentLeagueIndex = startLeagueIndex;
		LeaderBoardServer.instance.SendStars(num, GameController.instance.curTypeGame);
		CoroutineRunner.Instance.StartCoroutine(CupsCountAnimation(starstCups, 0));
		if (startLeague < CupsManager.CurrentLeague)
		{
			CoroutineRunner.Instance.StartCoroutine(LeagueUPAnimation());
		}
		else if (startLeague > CupsManager.CurrentLeague)
		{
			CoroutineRunner.Instance.StartCoroutine(LeagueDOWNAnimatio());
		}
		InfoMatchReward result = default(InfoMatchReward);
		result.id = "star";
		result.count = num;
		return result;
	}

	private void OnDisable()
	{
		x2bonus.SetActive(false);
	}

	private IEnumerator LeagueUPAnimation()
	{
		yield return new WaitForSeconds(0.75f);
		HOTween.To(leagueTexture, 1f, new TweenParms().Prop("alpha", 0).Ease(EaseType.EaseInOutExpo));
		HOTween.To(leagueTexture.transform, 1.5f, new TweenParms().Prop("localScale", Vector3.one * 0.15f).Ease(EaseType.EaseInOutExpo).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			leagueTexture.LoadTextureAndAlphaMask("LigasIcons/", CupsManager.CurrentLeague.ToString());
			leagueTexture.transform.localScale = Vector3.one * 1.5f;
			HOTween.To(leagueTexture, 0.6f, new TweenParms().Prop("alpha", 1).Ease(EaseType.EaseInOutExpo));
			HOTween.To(leagueTexture.transform, 1.5f, new TweenParms().Prop("localScale", Vector3.one).Ease(EaseType.EaseInOutElastic));
		}));
	}

	private IEnumerator LeagueDOWNAnimatio()
	{
		yield return new WaitForSeconds(0.75f);
		HOTween.To(leagueTexture, 1.5f, new TweenParms().Prop("alpha", 0).Ease(EaseType.EaseInOutExpo));
		HOTween.To(leagueTexture.transform, 2f, new TweenParms().Prop("localScale", Vector3.one * 2f).Ease(EaseType.EaseInOutExpo).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			leagueTexture.LoadTextureAndAlphaMask("LigasIcons/", CupsManager.CurrentLeague.ToString());
			leagueTexture.transform.localScale = Vector3.one * 0.15f;
			HOTween.To(leagueTexture, 2f, new TweenParms().Prop("alpha", 1).Ease(EaseType.EaseInOutElastic));
			HOTween.To(leagueTexture.transform, 2f, new TweenParms().Prop("localScale", Vector3.one).Ease(EaseType.EaseInOutElastic));
		}));
	}

	protected IEnumerator CupsCountAnimation(int start, int add)
	{
		cupsCount.text = string.Empty;
		newCupsCount.text = string.Empty;
		cups = start;
		addCups = Mathf.Abs(add);
		newCupsCount.color = Color.green;
		string prefix = string.Empty;
		if (add > 0)
		{
			newCupsCount.color = Color.green;
			prefix = "+ ";
		}
		else if (add < 0)
		{
			newCupsCount.color = Color.red;
			prefix = "- ";
		}
		cupsCount.text = string.Empty + cups;
		if (addCups != 0)
		{
			newCupsCount.text = string.Empty + prefix + addCups;
		}
		yield return new WaitForSeconds(2f);
		float anumationTime = Mathf.Clamp((float)addCups * 0.25f, 1f, 5f);
		HOTween.To(this, anumationTime, new TweenParms().Prop("cups", start + add).OnUpdate((TweenDelegate.TweenCallback)delegate
		{
			cupsCount.text = cups.ToString();
		}));
		if (addCups == 0)
		{
			yield break;
		}
		HOTween.To(this, anumationTime, new TweenParms().Prop("addCups", 0).OnUpdate((TweenDelegate.TweenCallback)delegate
		{
			if (addCups != 0)
			{
				newCupsCount.text = prefix + addCups;
			}
			else
			{
				newCupsCount.text = string.Empty;
				x2bonus.SetActive(false);
			}
		}));
	}
}
